package com.myk.game.gobangchess.rooms.prerooms;

import com.myk.game.gobangchess.CustomBottom;
import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.constants.ColorCost;
import com.myk.game.gobangchess.signs.BadgeSign;
import com.myk.game.gobangchess.signs.GroundSign;
import com.myk.game.gobangchess.signs.TextSign;
import com.myk.game.gobangchess.signs.TitleSign;
import com.myk.game.gobangchess.utils.GridsUtils;
import com.sun.deploy.panel.ITreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 圣留棋 //兵分棋 divide 行军  军事 兵法 march  army military affairs art of war
 */
public class AetheriaRoom extends Room {

    private List<Integer> blackHandChessList;//黑方手中持有棋子，上线4个且元素不同，3红：死， 4蓝：空， 5黄：时，  6绿：生
    private List<Integer> whiteHandChessList;//白方手中持有棋子，上线4个且元素不同 3红：死， 4蓝：空， 5黄：时，  6绿：生

    public AetheriaRoom(String roomName) {
        super(roomName, "江柯设计，包含可控随机性、无偏博弈、和连锁特效等元素的落子棋。", "圣留棋", GridsUtils.createEmptyGrids(15,15), true, true);
    }

    @Override
    public String getRuleText() {
        return "{{《圣留棋》Aetheria游戏规则@scale=1.3@color=#FF000000}}\n" +
                "{{一、棋盘与棋子设定：@style=i@color=#FF000000}}\n" +
                "\t\t1.采用 15×15 方格棋盘。\n" +
                "\t\t2.对局双方初始各持有 “{{生@color=#FF00AA00}}、{{死@color=#FFAA0000}}、{{时@color=#FFAAAA00}}、{{空@color=#FF0000AA}}” 四种属性棋子各 1 枚。\n" +
                "{{二、回合行动规则：@style=i@color=#FF000000}}\n" +
                "\t\t1.双方交替进行回合行动。每个回合中，玩家需先选定棋盘上的一处空格，再从己方持有的棋子中随机挑选 1 枚放置在选定位置。\n" +
                "\t\t2.当己方将最后 1 枚棋子放置到棋盘后，可立即重新获得 “{{生@color=#FF00AA00}}、{{死@color=#FFAA0000}}、{{时@color=#FFAAAA00}}、{{空@color=#FF0000AA}}” 4 枚棋子。\n" +
                "\t\t3.玩家在任何时刻，均可先补满 “{{生@color=#FF00AA00}}、{{死@color=#FFAA0000}}、{{时@color=#FFAAAA00}}、{{空@color=#FF0000AA}}” 这 4 枚棋子，再执行其他操作。\n" +
                "{{三、特殊规则说明：@style=i@color=#FF000000}}\n" +
                "\t\t1.棋子一旦落在棋盘上，即成为双方共有资源，不再区分归属。\n" +
                "\t\t2.落子后，需检查 3×3 范围内是否存在 5 枚特定同属性元素棋子相接，呈直轴对称排列的特殊形状。属性对应的特殊形状如下：\n" +
                "\t\t\t\t{{生：十字@color=#FF00AA00}}，{{死：X字@color=#FFAA0000}}，{{时：T字@color=#FFAAAA00}}，{{空：U字@color=#FF0000AA}}。\n" +
                "\t\t3.每当形成一次特殊形状，己方将获得 1 分，并移除组成该特殊形状的所有棋子，同时触发对应属性的特殊效果：\n" +
                "\t\t\t\t{{生：@color=#FF00AA00}}可选择场上任意 1 枚棋子，向其直向或斜向相邻的空格复制 1 枚相同棋子。\n" +
                "\t\t\t\t{{死：@color=#FFAA0000}}可移除场上任意 1 枚棋子。\n" +
                "\t\t\t\t{{时：@color=#FFAAAA00}}己方额外获得一次落子机会。\n" +
                "\t\t\t\t{{空：@color=#FF0000AA}}可将场上任意 1 枚棋子移动到棋盘上的另一个空格。\n" +
                "\t\t4.触发特效后，若棋盘上再次形成特殊形状，则可重复触发特效并累计得分。\n" +
                "{{四、胜利条件：@style=i@color=#FF000000}}\n" +
                "\t\t1.率先累积获得10分的一方获胜。\n" +
                "\t\t2.落子操作填满棋盘的一方获胜。";
    }

    @Override
    public String getGameInfo() {
        signList.clear();
        //绘制棋子上的文字
        for (int i = 0; i < curGrids.length; i++) {
            for (int j = 0; j < curGrids[i].length; j++) {
                if (curGrids[i][j] != 0){
                    signList.add(new TextSign(getIndex(i,j),getTextByValue(curGrids[i][j]),ColorCost.BLACK));
                }
            }
        }
        //绘制手上持有棋子
        if (blackHandChessList != null && whiteHandChessList != null){
            float aClipY = (curGrids.length + 0.2f) / curGrids.length;
            for (int i = 0; i < blackHandChessList.size(); i++) {
                int value = blackHandChessList.get(i);
                String text = getTipTextByValue(value);
                String color = getColorByValue(value);
                float bClipX = (i + 0.5f) / curGrids[0].length;;
                signList.add(new TitleSign(bClipX, aClipY, 0, text, color, 0.3f));
            }
            for (int i = 0; i < whiteHandChessList.size(); i++) {
                int value = whiteHandChessList.get(i);
                String text = getTipTextByValue(value);
                String color = getColorByValue(value);
                float wClipX = (curGrids[0].length - 4 + i + 0.5f) / curGrids[0].length;
                signList.add(new TitleSign(wClipX, aClipY, 0, text, color, 0.3f));
            }
        }
        return super.getGameInfo();
    }

    private String getColorByValue(int value) {
        switch (value) {
            case 3:
                return "#FFAA0000";
            case 4:
                return "#FF0000AA";
            case 5:
                return "#FFAAAA00";
            case 6:
                return "#FF00AA00";
            default:
                return "";
        }
    }

    private String getTipTextByValue(int value) {
        switch (value) {
            case 3:
                return "死(X)";
            case 4:
                return "空(U)";
            case 5:
                return "时(T)";
            case 6:
                return "生(+)";
            default:
                return "";
        }
    }

    private String getTextByValue(int value) {
        switch (value) {
            case 3:
                return "死";
            case 4:
                return "空";
            case 5:
                return "时";
            case 6:
                return "生";
            default:
                return "";
        }
    }

    @Override
    protected void onStartPrepare() {
        resetHandChessList(1);
        resetHandChessList(2);
        super.onStartPrepare();
    }

    @Override
    protected void prepareInGameButtons() {
        if (player_black != player_white) {
            player_black.registerCustomBottom(new CustomBottom(this, "重置属性", p -> resetHandChessList(1)));
            player_white.registerCustomBottom(new CustomBottom(this, "重置属性", p -> resetHandChessList(2)));
        }
        else {
            player_black.registerCustomBottom(new CustomBottom(this, "重置属性", p -> resetHandChessList(cur)));
        }
    }

    @Override
    public void doRule(int row, int col, int value) {
        if (value == 1){
            int index = (int) (Math.random()*blackHandChessList.size());
            int chess = blackHandChessList.get(index);
            curGrids[row][col] = chess;
            blackHandChessList.remove(index);
            if (blackHandChessList.size() == 0){
                resetHandChessList(1);
            }
            noticeWhiteMove();
        }
        else if (value == 2){
            int index = (int) (Math.random()*whiteHandChessList.size());
            int chess = whiteHandChessList.get(index);
            curGrids[row][col] = chess;
            whiteHandChessList.remove(index);
            if (whiteHandChessList.size() == 0){
                resetHandChessList(2);
            }
            noticeBlackMove();
        }
    }

    // 重置持有棋子
    private void resetHandChessList(int cur){
        List<Integer> list = new ArrayList<>();
        list.add(6);
        list.add(3);
        list.add(5);
        list.add(4);
        if (cur == 1){
            blackHandChessList = list;
        }
        else if (cur == 2){
            whiteHandChessList = list;
        }
        noticeAllRefreshGameInfo();
    }

}
